from flask import Flask
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.executors.pool import ProcessPoolExecutor

executors = {
    'default': ProcessPoolExecutor(3)
}
app = Flask(__name__)
app.scheduler = BackgroundScheduler(executors=executors)

# 定时任务
def print_tset(param):
    print(param)
# app.scheduler.add_job(print_tset, 'interval', seconds=3, args=['laowang'])


@app.route('/start')
def start():
    """
    启动定时任务
    :return:
    """
    app.scheduler.start()
    return '启动成功'

@app.route('/add_job/<param>')
def add_job(param):
    # 使用id指定定时任务名称
    app.scheduler.add_job(print_tset, 'interval', seconds=3, args=[param], id=param)
    return '动态添加定时任务成功: {}'.format(param)

@app.route('/pause_job/<param>')
def pause_job(param):
    # 暂停定时任务
    app.scheduler.pause_job(param)
    return '暂停定时任务成功: {}'.format(param)

@app.route('/resume_job/<param>')
def resume_job(param):
    # 恢复定时任务
    app.scheduler.resume_job(param)
    return '恢复定时任务成功: {}'.format(param)

@app.route('/shutdown')
def shutdown():
    # 关闭所有定时任务
    app.scheduler.shutdown()
    return '关闭所有定时任务成功'


if __name__ == '__main__':
    app.run(host='192.168.226.129', debug=True)

